(function($){


    $('label').click(function(){
        var t=$(this);
        if(t.hasClass('_dis')){
            return false;
        }
        var radio = t.find('input[type="radio"]');
            radio.attr('checked',true);
        var name=radio.attr('name');
        t.closest('form').find('input[name="'+name+'"]').closest('label').removeClass('_on');
        t.addClass('_on');
    })


    $('input[type="tel"]').each(function(){
        var t=$(this);
        t.attr('onafterpaste',"this.value=this.value.replace(/\D/g,'')");
        t.attr('pattern',"[0-9]");
    })

    $('input[type="tel"]').keyup(function(){
        var t=$(this);
        t.val(t.val().replace(/\D/g,''));
    })


    $('._btn_pop_kf').click(function(){
        PlayJsAdPopWin();
    })

    $('._btn_sms').each(function(){
        if(G.uat!=''){
            $(this).attr("href","sms:13168831555");
        }else{
            $(this).attr("href","javascript:alert('短信下单请发送：\\n--------------------------------------\\n收货地址 + 收货人 + 收货电话 \\n到：131 6883 1555 \\n--------------------------------------\\n我们收到短信后会与您联系确认 ');");
        }
        
    })

    $('._btn_tel').each(function(){
        if(G.uat!=''){
            $(this).attr("href","tel:13168831555");
        }else{
            $(this).attr("href","javascript:alert('电话订购请拨打免费热线：\\n--------------------------------------\\n 131 6883 1555 \\n--------------------------------------\\n');");
        }
        
    })

    //

    $(window).scroll(function(){
        fixed_footer_adjust();
    });
    fixed_footer_adjust();

    $('._btn_scroll_buy').click(function(){
        // $("html,body").animate({scrollTop:$("#a_order").offset().top},300);
        window.location.href = "#a_order";
    })

    $('._btn_top').click(function(){
        $("html,body").animate({scrollTop:0},300);
    })

    $('._btn_scroll_more').click(function(){
        // $("html,body").animate({scrollTop:$("#QGMS").offset().top},300);
        window.location.href = "#QGMS";
    })

    // var clearAdt = 0;
    var clearAd = setInterval(function(){
        $('iframe').hide();
        $('#close').hide();
        $('#bdstat').hide();
        $('#ad_colse_container').hide();
        $('[id]').each(function(){
            if( $(this).attr('id').indexOf('ad') == 0 && $(this).attr('id').indexOf('add') != 0 ){
                $(this).hide();
            }
        })
        // clearAdt ++;
    },500);



    // console.log(G);
    if(G.url.indexOf('item.php')>=0){
        $('._btn_scroll_more').show();
        $('._btn_scroll_buy').show();
    }



    if(G.uat!=''){

        $('.__mobile_show').show();
        $('.__pc_show').hide();

        //修复ios的focus时的问题
        $('input,textarea').focus(function(){
            $('.fixed-footer').css({position:'absolute','bottom':'auto'}).addClass('_ios-fixed-debug');
            fixed_footer_adjust();
        })
        $('input,textarea').blur(function(){
            $('.fixed-footer').css({position:'fixed','top':'auto','bottom':'0px'}).removeClass('_ios-fixed-debug');
            fixed_footer_adjust();
            
        })

    }else{
        $('.__mobile_show').hide();
        $('.__pc_show').show();
    }

})(jQuery);


function end(){
    var url = 'end_ajax.php';
    if( G.url.indexOf('?')>=0 ){
        var h = G.url;
        var a = G.url.split('?');
        var p = a[1];
        url = url + '?' + p + '';

        var _d = {};
            _d.url = h;
            _d.uat = G.ua;
            _d.refer = '';
            if( !!G.api && !!G.api.url && !!G.api.url.refer ){
                _d.refer = G.api.url.refer; 
            }
            
        $.ajaxPost({
            url:url,
            data:_d,
            success:function(rs){
                // if(G.debug){
                //     console.log(rs);
                // }

                if( !!rs.user_info && !!$('#form_order').attr('id') ){
                    // console.log(rs.user_info);

                    var form_order = $('#form_order');
                    if( !!rs.user_info.user_name && rs.user_info.user_name!='' ){
                        form_order.find('[name=name]').val(rs.user_info.user_name);
                    }
                    if( !!rs.user_info.user_mobile && rs.user_info.user_mobile!='' ){
                        form_order.find('[name=mobile]').val(rs.user_info.user_mobile);
                    }
                    if( !!rs.user_info.user_address && rs.user_info.user_address!='' ){
                        form_order.find('[name=address]').val(rs.user_info.user_address);
                    }

                    var the_address = {};
                    if( !!rs.user_info.user_province && rs.user_info.user_province!='' ){
                        the_address.province = rs.user_info.user_province;
                    }
                    if( !!rs.user_info.user_city && rs.user_info.user_city!='' ){
                        the_address.city = rs.user_info.user_city;
                    }
                    if( !!rs.user_info.user_area && rs.user_info.user_area!='' ){
                        the_address.area = rs.user_info.user_area;
                    }

                    $("#address").addressSelector(the_address);
                    
                }

                if( !!rs._ias && rs._ias == 1 && G.uat!='' ){

                    setTimeout(function(){
                        $('._item-detail-ads').show();
                    },100);
                    setTimeout(function(){
                        $('._item-detail-ads-pop').show();
                    },5000);
                    G._ias = true;
                    
                }else{
                    $('._item-detail-ads').remove();
                    G._ias = false;
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}

function delayEnd(){
    var url = 'delay_end_ajax.php';
    // console.log(G);

    if( G.url.indexOf('?')>=0 ){
        var h = G.url;
        var a = G.url.split('?');
        var p = a[1];
        url = url + '?' + p + '';

        var _d = {};
            _d.url = h;
            _d.uat = G.ua;
            _d.refer = '';
            if( !!G.api && !!G.api.url && !!G.api.url.refer ){
                _d.refer = G.api.url.refer; 
            }
             
        $.ajaxPost({
            url:url,
            data:_d,
            success:function(rs){
                if(G.debug){
                    console.log(rs);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}

//
// setTimeout(function(){
//     // delayEnd();
// },200);

//
function PlayJsAdPopWin(){

    if( G.uat!='' || 1){
        window.open('http://wpa.qq.com/msgrd?v=3&uin=3261203709&site=qq&menu=yes','_blank');

    //
    }else{
        var url = 'qq_ajax.php';
        var _d = {};
            _d.qq = '3261203709';
            _d.page_url = G.url;

            var temp = G.url.split('?');
            if( !!temp[1] ){
                temp = temp[1].split('&');
                if( !!temp && typeof temp == 'object' ){
                    for(var i in temp){
                        var v = temp[i];
                        if(v.indexOf('id=') == 0){
                            var id = v.replace('id=','');
                            id = id*1;
                            break;
                        }
                    }
                }
            }

            //
            if( !!id && G.reg.id.test(id) ){
                _d.title = '商品编号：'+id;
            }

            //
            $.ajaxPost({
                url:url,
                data:_d,
                success:function(rs){
                    if( !!rs.data.sign && rs.data.sign.indexOf('tencent://') == 0 ){
                        popwin = window.location.href = rs.data.sign;
                    }else{
                        window.open('http://wpa.qq.com/msgrd?v=3&uin=3261203709&site=qq&menu=yes','_blank');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    window.open('http://wpa.qq.com/msgrd?v=3&uin=3261203709&site=qq&menu=yes','_blank');
                    
                }
            });




        // popwin=window.location.href="tencent://message/?Menu=yes&amp;uin=800132181&amp;Service=58&amp;SigT=A7F6FEA02730C988457765F40057A1C323B9BF76ECBA2719B4DDEDA76665E40BA1CA7B2B59399BDDA4FDB4752DEB04FA4B372E5C764A95C3F8C3CDD2BEEB5ECA2A4BE91D8D86FA5DD8FFBDB4FCB0392B53D8EFC01A989561E1D9A791978743F8B4D940AB411EE96AC27FBFFAC5D1C0121D385B6BEDEB419B&amp;SigU=30E5D5233A443AB2265A4C6D73160EC324110095DE67623EE68A5FFD27D5B4038A31373852B8DD667940ED78DA844086F41697C6092E423EA2DD4A04106916762B989FA13DC123DC";
    }

 };
// setTimeout("PlayJsAdPopWin()",1000);



//调整底部固定条的显示与否
function fixed_footer_adjust(){
    var w = $(window).scrollTop();
    var wh = $(window).height();

    if($("#a_order").length){
        var o = $("#a_order").offset().top;
        var oh = $("#form_order").height();

        var s = w-(o-wh);
        // console.log('s='+s);
        // console.log('o='+o);

        if(s>200&&s<(oh+80)){
            $('.fixed-footer').slideUp();
        }else{
            $('.fixed-footer').slideDown();
        }
    }


    if(w>0){
        $('._btn_top').fadeIn();
    }else{
        $('._btn_top').fadeOut();
    }


}

